package com.huayun.viewutils.iconview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.support.v4.util.ArrayMap;
import android.webkit.WebView;


import com.huayun.ViewUtils;
import com.huayun.util.utils.FileUtil;
import com.huayun.util.utils.Utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

/**
 * Description:
 * Created on 2019/8/19 0019 14:31:12
 * Organization:华云
 * author:AHuangSHang
 */
public class ParseResourceUtil {
    public HashMap<String, Object> iconData = new HashMap<>();
    private HashMap<String, Object> stringData = new HashMap<>();
    public Typeface typeface;
    public final static File resourcePath = FileUtil.getCacheDirectory(ViewUtils.getApplicationContext(), "huaYun");
    private ParseResourceUtil() {
        readLocalJson("iconfont");  //读取字体json并存入map
        readLocalJson("strings");   //读取strings资源并存入map
        readLocalTTF();
    }

    private static class SingletonHolder {
        private static final ParseResourceUtil INSTANCE = new ParseResourceUtil();
    }

    public static ParseResourceUtil getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public String getString(String key) {
        if (!ParseResourceUtil.getInstance().stringData.containsKey(key))
            return key;
        return ParseResourceUtil.getInstance().stringData.get(key).toString();
    }

    private String readLocalJson(String fileName) {
        String resultString = "";
        InputStream inputStream;
        try {
            File file = new File(resourcePath + "/" + fileName);
            if (file.exists())
                inputStream = new FileInputStream(file);
            else
                inputStream = ViewUtils.getApplicationContext().getResources().getAssets().open(fileName);

            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            resultString = new String(buffer, "UTF-8");
            //TODO:获取解密后的东西
            //resultString = SecurityUtil.decodeAES(resultString);
            if (fileName.equals("iconfont")) {
                iconData = Utils.jsonToMap(resultString);
            } else {
                stringData = Utils.jsonToMap(resultString);
            }
            return resultString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultString;
    }

    private void readLocalTTF() {
        File file = new File(resourcePath + "/iconfont.ttf");
        try {
            if (file.exists()) {
                typeface = Typeface.createFromFile(file);
            } else {
                typeface = Typeface.createFromAsset(ViewUtils.getApplicationContext().getAssets(), "iconfont.ttf");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载资源包中的html
     */
    public void loadResourceHtml(String fileName, WebView webView) {
        InputStream inputStream = null;
        File file = new File(resourcePath + "/views/" + fileName);
        String baseURL;
        try {
            if (file.exists()) {
                inputStream = new FileInputStream(file);
                baseURL = "file://" + resourcePath + "/views/static";
            } else {
                inputStream = ViewUtils.getApplicationContext().getResources().getAssets().open("views/" + fileName);
                baseURL = "file:///android_asset/views/static";
            }
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            webView.loadDataWithBaseURL(baseURL, new String(bytes), "text/html", "UTF-8", "");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 加载资源包中的图片
     */
    public Bitmap getResourceBitMap(String fileName) {
        Bitmap bit = null;
        InputStream inputStream = null;
        File file = new File(resourcePath + "/img/" + fileName);
        try {
            if (file.exists()) {
                inputStream = new FileInputStream(file);
                bit = BitmapFactory.decodeStream(inputStream);

            } else {
                inputStream = ViewUtils.getApplicationContext().getResources().getAssets().open("img/" + fileName);
                bit = BitmapFactory.decodeStream(inputStream);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bit;
    }
}